package com.jscn.hwdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnScheme = findViewById(R.id.btnScheme);
        Button btnActivity = findViewById(R.id.btnActivity);

        btnScheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchViaScheme();
            }
        });

        btnActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchViaActivity();
            }
        });
    }

    private void launchViaScheme() {
        Uri uri = new Uri.Builder()
                .scheme("esapp")
                .authority("action")
                .appendPath("start")
                .appendQueryParameter("pkg", "es.com.maoyan.tv")
                .appendQueryParameter("from", "demo_app")
                .build();

        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setPackage(this.getPackageName());
        startActivity(intent);
    }

    private void launchViaActivity() {
        String pkgName = this.getPackageName();
        String action = pkgName + ".ES_ACTION_LAUNCH";
        Intent intent = new Intent(action);
        intent.putExtra("pkg", BuildConfig.ES_APP_PKG);
        intent.putExtra("args", BuildConfig.ES_APP_ARGS);
        intent.putExtra("from", "demo_app");
        intent.setPackage(pkgName);
        startActivity(intent);
    }
}